﻿Public Class SessionMessage
    Inherits MessageBase

    Public Property MessageIndex As Integer?
    Public Property Timestamp As Date?
    Public Property [Event] As EventEnum?
    Public Property EventType As EventTypeEnum?
    Public Property StartTime As Date?
    Public Property StartPositionLatitude As Integer?
    Public Property StartPositionLongitude As Integer?
    Public Property Sport As SportEnum?
    Public Property SubSport As SubSportEnum?
    Public Property TotalElapsedTime As Decimal?
    Public Property TotalTimerTime As Decimal?
    Public Property TotalDistance As Decimal?
    Public Property TotalCycles As UInt32?
    Public Property TotalStrides As UInt32?
    Public Property TotalCalories As Integer?
    Public Property TotalFatCalories As Integer?
    Public Property AvgSpeed As Decimal?
    Public Property MaxSpeed As Decimal?
    Public Property AvgHeartRate As Integer?
    Public Property MaxHeartRate As Integer?
    Public Property AvgCadence As UInt32?
    Public Property AvgRunningCadence As UInt32?
    Public Property MaxCadence As UInt32?
    Public Property MaxRunningCadence As UInt32?
    Public Property AvgPower As Integer?
    Public Property MaxPower As Integer?
    Public Property TotalAscent As Integer?
    Public Property TotalDescent As Integer?
    Public Property TotalTrainingEffect As Integer?
    Public Property FirstLapIndex As Integer?
    Public Property NumLaps As Integer?
    Public Property EventGroup As Integer?
    Public Property Trigger As SessionTriggerEnum?
    Public Property NecLatitude As Integer
    Public Property NecLongitude As Integer
    Public Property SwcLatitude As Integer
    Public Property SwcLongitude As Integer


    Protected Overrides Sub Populate(reader As System.IO.BinaryReader, Definition As DefinitionMessage)
        For Each FieldDef As FieldDefinition In Definition.Fields
            Dim size As Integer = FieldDef.Size
            Dim FieldDefNumber As Integer = FieldDef.FieldDefinitionNumber
            Dim FieldType As Integer = FieldDef.BaseType
            Select Case FieldDef.FieldDefinitionNumber
                Case 254
                    MessageIndex = reader.ReadUInt16
                Case 253
                    Timestamp = FitHelper.TranslateDateTime(reader.ReadUInt32)
                Case 0
                    [Event] = CType(reader.ReadByte, EventEnum)
                Case 1
                    EventType = CType(reader.ReadByte, EventTypeEnum)
                Case 2
                    StartTime = FitHelper.TranslateDateTime(reader.ReadUInt32)
                Case 3
                    StartPositionLatitude = reader.ReadInt32
                Case 4
                    StartPositionLongitude = reader.ReadInt32
                Case 5
                    Sport = CType(reader.ReadByte, SportEnum)
                Case 6
                    SubSport = CType(reader.ReadByte, SubSportEnum)
                Case 7
                    TotalElapsedTime = CDec(reader.ReadUInt32 / 1000)
                Case 8
                    TotalTimerTime = CDec(reader.ReadUInt32 / 1000)
                Case 9
                    TotalDistance = CDec(reader.ReadUInt32 / 100)
                Case 10
                    If Sport = SportEnum.FIT_SPORT_RUNNING Then
                        TotalStrides = reader.ReadUInt32
                    Else
                        TotalCycles = reader.ReadUInt32
                    End If
                Case 11
                    TotalCalories = reader.ReadUInt16
                Case 13
                    TotalFatCalories = reader.ReadUInt16
                Case 14
                    AvgSpeed = CDec(reader.ReadUInt16 / 1000)
                Case 15
                    MaxSpeed = CDec(reader.ReadUInt16 / 1000)
                Case 16
                    AvgHeartRate = reader.ReadByte
                Case 17
                    MaxHeartRate = reader.ReadByte
                Case 18
                    If Sport = SportEnum.FIT_SPORT_RUNNING Then
                        AvgCadence = reader.ReadByte
                    Else
                        AvgRunningCadence = reader.ReadByte
                    End If
                Case 19
                    If Sport = SportEnum.FIT_SPORT_RUNNING Then
                        MaxCadence = reader.ReadByte
                    Else
                        MaxRunningCadence = reader.ReadByte
                    End If
                Case 20
                    AvgPower = reader.ReadUInt16
                Case 21
                    MaxPower = reader.ReadUInt16
                Case 22
                    TotalAscent = reader.ReadUInt16
                Case 23
                    TotalDescent = reader.ReadUInt16
                Case 24
                    TotalTrainingEffect = CInt(reader.ReadByte / 10)
                Case 25
                    FirstLapIndex = reader.ReadUInt16
                Case 26
                    NumLaps = reader.ReadUInt16
                Case 27
                    EventGroup = reader.ReadByte
                Case 28
                    Trigger = CType(reader.ReadByte, SessionTriggerEnum)
                Case 29
                    NecLatitude = reader.ReadInt32
                Case 30
                    NecLongitude = reader.ReadInt32
                Case 31
                    SwcLatitude = reader.ReadInt32
                Case 32
                    SwcLongitude = reader.ReadInt32
                Case Else
                    FitHelper.ReadUnknownField(reader, FieldDef)
            End Select
        Next
    End Sub
End Class
